﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    Layout = "_Main";
}
@model ManagerWeb.Models.ModelBuilderViewModel

    <div class="row">
        <div class="col-md-6" style="width: 80%;">
            <div class="box box-info">
                <form class="form-horizontal">
                    <div class="box-body">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">数据库类型：</label>
                            <div class="col-sm-10">
                                @Html.DropDownListFor(model => model.DatabaseType, ViewData["DatabaseTypeList"] as SelectList, new { @class = "form-control" })
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">服务器地址/IP：</label>
                            <div class="col-sm-10">
                                <input type="text" id="Server" class="form-text" placeholder="例如：127.0.0.1" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">端口号：</label>
                            <div class="col-sm-10">
                                <input type="text" id="Port" class="form-text" placeholder="例如：3306" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户名：</label>
                            <div class="col-sm-10">
                                <input type="text" id="UserName" class="form-text" placeholder="例如：root" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">密码：</label>
                            <div class="col-sm-10">
                                <input type="password" id="Password" class="form-text" placeholder="例如：123456" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">数据库：</label>
                            <div class="col-sm-8">
                                <select class="form-control" id="slDatabase">
                                    <option value="">请选择...</option>
                                </select>
                            </div>
                            <div class="col-sm-2">
                                <input type="button" class="btn btn-primary pull-right" value="获取数据库" id="btnGetDatabase" />
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">表名称：</label>
                            <div class="col-sm-10">
                                <select class="form-control" id="slTable">
                                    <option value="">请选择...</option>
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="box-footer">
                        
                        @*<input type="button" class="btn btn-primary pull-right" value="获取所有表" id="btnGetTable" style="margin-right: 20px;" />*@
                        <input type="button" class="btn btn-primary pull-right" value="生成代码" id="btnBuild" />
                    </div>
                </form>

            </div>
        </div>
    </div>
@section Footer{

    <script type="text/javascript">
        $(function () {

            $("#btnGetDatabase").click(function () {
                var DatabaseType = $("#DatabaseType").val();
                var Server = $("#Server").val();
                var Port = $("#Port").val();
                var UserName = $("#UserName").val();
                var Password = $("#Password").val();
                var data = JSON.stringify({ "DatabaseType": DatabaseType, "Server": Server, "Port": Port, "UserName": UserName, "Password": Password });
                var getDatabaseUrl = "@Url.Content("~/ModelBuilder/GetDatabase")";
                SendHttpRequest("POST", getDatabaseUrl, "json", data, function (result) {
                    if (result != undefined && result.length > 0) {
                        $.each(result, function (name, value) {
                            $("#slDatabase").append("<option value='" + value + "'>" + value + "</option>");
                        });
                    }
                });
            });

            $("#slDatabase").change(function () {
                    //$("#slTable").empty();
                $("#slTable option:not(:first)").remove();
                var DatabaseType = $("#DatabaseType").val();
                var Server = $("#Server").val();
                var Port = $("#Port").val();
                var UserName = $("#UserName").val();
                var Password = $("#Password").val();
                var database = $("#slDatabase").val();
                if (database == '') {
                    ShowErrorMessage("请选择数据库")
                    return false;
                }

                var data = JSON.stringify(
                    {
                        "DatabaseType": DatabaseType, "Server": Server
                        , "Port": Port, "UserName": UserName, "Password": Password
                        , "DatabaseName": database
                    }
                );
                var getDatabaseUrl = "@Url.Content("~/ModelBuilder/GetTable")";
                SendHttpRequest("POST", getDatabaseUrl, "json", data, function (result) {
                    if (result != undefined && result.length > 0) {
                        $.each(result, function (name, value) {
                            $("#slTable").append("<option value='" + value + "'>" + value + "</option>");
                        });
                    }
                });
                return true;
            });


            $("#btnBuild").click(function () {
                var DatabaseType = $("#DatabaseType").val();
                var Server = $("#Server").val();
                var Port = $("#Port").val();
                var UserName = $("#UserName").val();
                var Password = $("#Password").val();
                var database = $("#slDatabase").val();
                var tableName = $("#slTable").val();
                if (database == '') {
                    ShowErrorMessage("请选择数据库")
                    return false;
                }
                if (tableName == '') {
                    ShowErrorMessage("请选择表名称")
                    return false;
                }
                var data = JSON.stringify(
                    {
                        "DatabaseType": DatabaseType, "Server": Server
                        , "Port": Port, "UserName": UserName, "Password": Password
                        , "DatabaseName": database, "TableName": tableName
                    }
                );
                var getDatabaseUrl = "@Url.Content("~/ModelBuilder/Build")";
                SendHttpRequest("POST", getDatabaseUrl, "html", data, function (result) {
                    //layer.msg(result);
                    //if (result != undefined && result.length > 0) {
                    //    $.each(result, function (name, value) {
                    //        $("#slTable").append("<option value='" + value + "'>" + value + "</option>");
                    //    });
                    //}
                    layer.open({
                        type: 1,
                        title: "生成的 Model 代码",
                        closeBtn: 1,
                        shadeClose: true,
                        area: ['760px', '650px'],
                        //skin: 'yourclass',
                        //btn: ['确定', '取消'],
                        //btn1: function () {
                        //    ShowCode();
                        //  }
                        //  ,btn2: function(){
                        //    Copy(this);
                        //  },
                        content: result
                    });
                    //  layer.open({
                    //    type: 2,
                    //    title: '详 情',
                    //    area: ['700px', '500px'],
                    //    fixed: false, //不固定
                    //    maxmin: false,
                    //    content: '../MonitorLog/Detail?id=' + id
                    //});

                    //return false;
                });
                //window.location.href = "https://localhost:44395/ModelBuilder/Build?model=" + data;
                //layer.open({
                //      type: 2,
                //      area: ['700px', '450px'],
                //      fixed: false, //不固定
                //      maxmin: true,
                //      content: "https://localhost:44395/ModelBuilder/Build?model=" + data
                //    });

            });

        });
    </script>

}